package org.webby.url;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class ObjectATag extends SimpleTagSupport {
  private WebAddressable object;

  public void setObject(WebAddressable object) {
    this.object = object;
  }

  @Override
  public void doTag() throws JspException, IOException {
    StringBuilder sb = new StringBuilder();
    sb.append("<a href='");
    sb.append(object.getPath());
    sb.append("'>");
    String titleText = object.getTitleText();
    if (titleText != null) {
      sb.append(titleText);
    } else {
      sb.append("(untitled)");
    }
    sb.append("</a>");
    getJspContext().getOut().write(sb.toString());
  }
}
